import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:photo_manager/photo_manager.dart';

import '/app/pages/video_play/video_player_page.dart'; // 导入 GetX 进行导航

class VideoCard extends StatelessWidget {
  final Widget? image;
  final String? videoName;
  final String? currentTime;
  final String? totalTime;
  final bool isRowMode; // 控制展示方式
  final AssetEntity assetEntity; // 视频文件的 AssetEntity 对象
  final List<AssetEntity>? all; // 全部视频文件列表

  const VideoCard({
    super.key,
    this.videoName,
    this.currentTime,
    this.totalTime,
    this.image,
    required this.assetEntity,
    this.all,
    this.isRowMode = false, // 默认为列展示
  });

  @override
  Widget build(BuildContext context) {
    // 使用 GestureDetector 包裹整个卡片，捕捉点击事件
    return GestureDetector(
      onTap: () {
        // all 为空时 初始化一个 List<AssetEntity>
        List<AssetEntity> tem = all ?? [];
        // 点击时导航到视频播放页面
        Get.to(() => VideoPlayerPage(videoFile: assetEntity, videoList: tem));
      },
      child: isRowMode ? _buildRowMode() : _buildColumnMode(),
    );
  }

  // 列展示模式
  Widget _buildColumnMode() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 5), // 增加左右边距
      width: 150, // 卡片宽度
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          AspectRatio(
            aspectRatio: 16 / 9, // 16:9 比例
            child: ClipRRect(
              borderRadius: BorderRadius.circular(8), // 圆角边框
              child: image ?? Container(color: Colors.grey), // 处理 image 为空的情况
            ),
          ),
          SizedBox(height: 8),
          Text(
            videoName ?? "Unknown Video",
            style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          SizedBox(height: 4),
          Text(
            "$currentTime / $totalTime",
            style: TextStyle(color: Colors.black38, fontSize: 10, fontWeight: FontWeight.w500),
          ),
        ],
      ),
    );
  }

  // 单行展示模式
  Widget _buildRowMode() {
    return Container(
      padding: EdgeInsets.all(8),
      margin: EdgeInsets.symmetric(horizontal: 5),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Container(
              width: 100,
              height: 60,
              color: Colors.grey,
              child: image ?? Container(color: Colors.grey),
            ),
          ),
          SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  videoName ?? "Unknown Video",
                  style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
                SizedBox(height: 4),
                Text(
                  "$currentTime / $totalTime",
                  style: TextStyle(color: Colors.black38, fontSize: 10, fontWeight: FontWeight.w500),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
